package com.appspot.ldsdrawingtool.server.services;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import com.appspot.ldsdrawingtool.server.lds.components.LDSModel;

public final class LDSModelStorageService {

    private static final PersistenceManagerFactory lmssInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private LDSModelStorageService() {}

    public static PersistenceManagerFactory get() {
        return lmssInstance;
    }

    public static LDSModel getModelByIdentifier(String identifier) {
        // Create a new persistence manager for every request
        PersistenceManager pm = lmssInstance.getPersistenceManager();
        Object storedObject = null;
        try {
         storedObject = pm.getObjectById(LDSModel.class, identifier);
        } catch (Exception e) {
        }
        if (storedObject instanceof LDSModel) {
            return (LDSModel)storedObject;
        }
        return null;
    }

    public static void persistLDSModel(Object objectToPersist) {
        if (objectToPersist != null) {
            PersistenceManager pm = lmssInstance.getPersistenceManager();
            pm.makePersistent(objectToPersist);
        }
    }

}
